package com.example.take_out_dish.service.impl;

import com.example.take_out_dish.config.JwtConfig;
import com.example.take_out_dish.entity.Setmeal;
import com.example.take_out_dish.exceptions.BusinessException;
import com.example.take_out_dish.exceptions.enumetation.ResponseEnum;
import com.example.take_out_dish.mapper.SetmealMapper;
import com.example.take_out_dish.service.SetmealService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.take_out_dish.utils.JwtUtils;
import io.jsonwebtoken.Claims;
import jakarta.annotation.Resource;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 * 套餐 服务实现类
 * </p>
 *
 * @author wuyunbin
 * @since 2024-07-01
 */
@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {
    @Resource
    HttpServletRequest request;
    @Resource
    JwtConfig jwtConfig;
    @Override
    public void updateStatus(Setmeal one) {
        String token = "";
        Cookie[] cookies = request.getCookies();
        if (cookies != null) {
            for (Cookie cookie : cookies) {
                if (cookie.getName().equals("token")) {
                    token = cookie.getValue();
                }
            }
        }

        if (!JwtUtils.validateToken(token, jwtConfig.getKey())) {
            throw new BusinessException(ResponseEnum.TOKEN_EXPIRED);
        }
        Claims claims = JwtUtils.parseJwt(token, jwtConfig.getKey());
        String name = claims.get("name", String.class);
        one.setCreateUser(name);
        one.setUpdateUser(name);

        Date now=new Date();
        one.setCreateTime(now);
        one.setUpdateTime(now);
        this.updateById(one);
    }
}
